﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using JinfuyuanProject.Entity;
using JinfuyuanProject.Service.Interface;
using JinfuyuanProject.Repository.Interface;
using System.Collections;
using JinfuyuanProject.Entity.Filter;
using JinfuyuanProject.Entity.ViewModels;
using DocumentFormat.OpenXml.Drawing.Charts;
using System.Diagnostics.Contracts;
using DocumentFormat.OpenXml.Office2010.Excel;
using System.Reflection;
namespace JinfuyuanProject.Service
{
    public class InboundService : IInboundService
    {
        private readonly IInboundRepository _inboundRepository;
        private readonly IOutboundRepository _outboundRepository;

        public InboundService(IInboundRepository inboundRepository, IOutboundRepository outboundRepository)
        {
            _inboundRepository = inboundRepository;
            _outboundRepository = outboundRepository;
        }
        #region 基本方法
        public bool Delete(Inbound inbound)
        {
            return _inboundRepository.Delete(inbound);
        }

        public IQueryable<Inbound> GetList()
        {
            return _inboundRepository.GetList();
        }

        public Inbound GetModel(int Id)
        {
            return _inboundRepository.GetModel(Id);
        }

        public int Insert(Inbound inbound)
        {
            return _inboundRepository.Insert(inbound);
        }

        public bool Update(Inbound inbound)
        {
            return _inboundRepository.Update(inbound);
        }
        public IEnumerable<T> QueryBySql<T>(string sql)
        {
            return _inboundRepository.QueryBySql<T>(sql);
        }
        #endregion

        #region 业务方法
        public Hashtable QueryBound(InOutboundFilter filter)
        {
            int count;

            //使用SQL查询出入库记录（包含关联表）
            string inboundSql = @"
            select i.*,s.Name,p.ProductName,pa.VerifyId,c.ContractNumber,pa.ApplicationId as ApplyNameId,r.ParturientName,f.Name as ApplyName from Inbound i  

                                                                                  LEFT JOIN  Staff s  on i.InboundId=s.Id 
                                                                                  LEFT JOIN  Product  p on i.ProductId=p.Id
                                                                                  LEFT JOIN  ProductApplication pa  on i.ApplicationId = pa.Id
                                                                                  LEFT JOIN   Contract   c  on pa.ContractId=c.Id
                                                                                  LEFT JOIN  Parturient  r on c.ParturientId=r.Id
                                                                                  LEFT JOIN  Staff   f  on pa.ApplicationId  =f.Id";

            var inboundRecords = QueryBySql<InOutboundVM>(inboundSql).AsQueryable();

            Hashtable hashtable = new Hashtable();
            hashtable["code"] = 0;
            hashtable["message"] = "success";
            hashtable["data"] = MakeQueryFilter(inboundRecords, filter, out count);
            hashtable["count"] = count;//结束惰性加载

            return hashtable;
        }
        public IQueryable<InOutboundVM> MakeQueryFilter(IQueryable<InOutboundVM> inOutboundVMs, InOutboundFilter inOutboundFilter, out int count)
        {
            // 商品名称筛选
            if (!string.IsNullOrEmpty(inOutboundFilter.ProductName))
            {
                inOutboundVMs = inOutboundVMs.Where(x => x.ProductName.Contains(inOutboundFilter.ProductName));
            }

            // 操作人筛选
            if (!string.IsNullOrEmpty(inOutboundFilter.ApplyName))
            {
                inOutboundVMs = inOutboundVMs.Where(x => x.Name.Contains(inOutboundFilter.ApplyName));
            }

            // 类型筛选
            if (inOutboundFilter.Type.HasValue)
            {
                inOutboundVMs = inOutboundVMs.Where(x => x.Type == inOutboundFilter.Type.Value);
            }

            // 时间范围筛选
            if (inOutboundFilter.startTime.HasValue)
            {
                inOutboundVMs = inOutboundVMs.Where(x => x.InboundTime >= inOutboundFilter.startTime.Value);
            }

            if (inOutboundFilter.endTime.HasValue)
            {
                // 结束日期包含当天所有时间
                var endDate = inOutboundFilter.endTime.Value.Date.AddDays(1).AddSeconds(-1);
                inOutboundVMs = inOutboundVMs.Where(x => x.InboundTime <= endDate);
            }

            if (inOutboundFilter.IsDelete != null)
            {
                inOutboundVMs = inOutboundVMs.Where(x => x.IsDelete == inOutboundFilter.IsDelete);
            }
            //需要过滤后没做分页前的所有数据条数
            count = inOutboundVMs.Count();
            //做分页
            if (inOutboundFilter.page != 0 && inOutboundFilter.limit != 0)
                return inOutboundVMs.OrderBy(x => x.Id).Skip((inOutboundFilter.page - 1) * inOutboundFilter.limit).Take(inOutboundFilter.limit);
            else
                return inOutboundVMs;
        }


        #endregion
    }
}
